#include <my_header.h>
#include "pathStack.h"


int pathStackInit(pathStack_t *pathStack)
{
    bzero(pathStack,sizeof(pathStack_t));
    return 0;
}

//进栈
int pathStackPush(pathStack_t *pathStack,char* name)
{
    sNode_t *pNew = (sNode_t*)calloc(1,sizeof(sNode_t));
    
    strcpy(pNew->pathname,name);
    
    /* printf("%s\n",pNew->pathname); */
    if(pathStack->stackNum == 0)
    {
        pathStack->bottom = pNew;
        pathStack->top = pNew;
    }
    else
    {
        pathStack->top->next = pNew;
        pNew->prev = pathStack->top;
        pathStack->top = pNew;
    }
    ++pathStack->stackNum;
    /* printf("%s\n",pathStack->top->pathname); */
    return 0;
}

//出栈
int pathStackPop(pathStack_t *pathStack)
{
    if(pathStack->stackNum == 0)
    {
        return -1;
    }

    if(pathStack->stackNum == 1)
    {
        free(pathStack->top);
        pathStack->top = NULL;
        pathStack->bottom = NULL;
        --pathStack->stackNum;
        return 0;
    }    
    
    sNode_t *cur = pathStack->top; 
    cur->prev->next = NULL;
    pathStack->top = cur->prev;
    --pathStack->stackNum;
    free(cur);
    return 0;
}

//清空栈
int pathStackClear(pathStack_t *pathStack)
{
    while(pathStack->stackNum != 0)
    {
        pathStackPop(pathStack);
    }
    return 0;
}
//打印栈
int printPathStack(pathStack_t *pathStack)
{
    sNode_t *cur = pathStack->bottom;
    while(cur)
    {
        printf("%s\t",cur->pathname);
        cur = cur->next;    
    }
    printf("\n");
    return 0;
}

#if 0
int main()
{
    pathStack_t pathStack;
    
    pathStackInit(&pathStack);

    pathStackPush(&pathStack,"how");
    pathStackPush(&pathStack,"are");
    pathStackPush(&pathStack,"you");
    pathStackPush(&pathStack,"you");
    pathStackPush(&pathStack,"you");
    pathStackPush(&pathStack,"you");

    printf("--------------\n");
    printPathStack(&pathStack);


    /* printf("=================\n"); */
    /* pathStackClear(&pathStack); */
    /* printPathStack(&pathStack); */
    printf("=================\n");

    printf("--------------\n");
    pathStackPop(&pathStack);
    printPathStack(&pathStack);

    printf("--------------\n");
    pathStackPop(&pathStack);
    printPathStack(&pathStack);

    printf("--------------\n");
    pathStackPop(&pathStack);
    printPathStack(&pathStack);

    /* printf("--------------\n"); */
    /* pathStackPop(&pathStack); */
    /* printPathStack(&pathStack); */
    return 0;
}
#endif
